﻿using Microsoft.EntityFrameworkCore.Design;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FinalItem.MyEartags
{
    public class MyEartagsMigrationsDbContextFactory : IDesignTimeDbContextFactory<MyEartagsMigrationsDbContext>
    {
        public MyEartagsMigrationsDbContext CreateDbContext(string[] args)
        {
            var configuration = BuildConfiguration();

            var builder = new DbContextOptionsBuilder<MyEartagsMigrationsDbContext>()
                .UseMySql(configuration.GetConnectionString("MyEartags"),
                    new MySqlServerVersion(new Version(8, 0, 0)));
            return new MyEartagsMigrationsDbContext(builder.Options);
        }

        private static IConfigurationRoot BuildConfiguration()
        {
            var builder = new ConfigurationBuilder()
                .SetBasePath(Path.Combine(Directory.GetCurrentDirectory(), "../FinalItem.DbMigrator/"))
                .AddJsonFile("appsettings.json", optional: false);

            return builder.Build();
        }
    }
}
